﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolManagementSystem.Controls;
using SchoolManagementSystem.DetailsManager;
using SchoolManagementSystem.Forms;
using DevExpress.XtraBars;
using System.Reflection;
using SchoolManagementSystem.Details;
using CoreData;
using ActionServices;
using System.IO;
using Helper;
using SchoolManagementSystem.Forms.BusinessForm;
using SchoolManagementSystem.Searchs;
using System.Threading;
using System.Deployment.Application;

namespace SchoolManagementSystem
{
    public partial class frmMain : DevExpress.XtraEditors.XtraForm
    {
        public frmMain()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            this.AllowFormSkin = true;
            //SkinHelper.InitSkinGallery(this.skin, true);
            SetupSecurity();
            DevExpress.UserSkins.BonusSkins.Register();
            BarItem item = new Layout.BarLookAndFeelListItem(DevExpress.LookAndFeel.UserLookAndFeel.Default);
           
            bar_lookandfeel.AddItem(item);
           
            barCheckItem_VN.Checked = true;
            RestoreLayout();
           
        }

        private void InstallUpdateSyncWithInfo()
        {
            UpdateCheckInfo info = null;

            if (ApplicationDeployment.IsNetworkDeployed)
            {
                ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;

                try
                {
                    info = ad.CheckForDetailedUpdate();

                }
                catch (DeploymentDownloadException dde)
                {
                    MessageBox.Show("The new version of the application cannot be downloaded at this time. \n\nPlease check your network connection, or try again later. Error: " + dde.Message);
                    return;
                }
                catch (InvalidDeploymentException ide)
                {
                    MessageBox.Show("Cannot check for a new version of the application. The ClickOnce deployment is corrupt. Please redeploy the application and try again. Error: " + ide.Message);
                    return;
                }
                catch (InvalidOperationException ioe)
                {
                    MessageBox.Show("This application cannot be updated. It is likely not a ClickOnce application. Error: " + ioe.Message);
                    return;
                }

                if (info.UpdateAvailable)
                {
                    Boolean doUpdate = true;

                    if (!info.IsUpdateRequired)
                    {
                        DialogResult dr = MessageBox.Show("An update is available. Would you like to update the application now?", "Update Available", MessageBoxButtons.OKCancel);
                        if (!(DialogResult.OK == dr))
                        {
                            doUpdate = false;
                        }
                    }
                    else
                    {
                        // Display a message that the app MUST reboot. Display the minimum required version.
                        MessageBox.Show("This application has detected a mandatory update from your current " +
                            "version to version " + info.MinimumRequiredVersion.ToString() +
                            ". The application will now install the update and restart.",
                            "Update Available", MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
                    }

                    if (doUpdate)
                    {
                        try
                        {
                            splashScreenManager1.ShowWaitForm();
                            ad.Update();
                            splashScreenManager1.CloseWaitForm();
                            MessageBox.Show("The application has been upgraded, and will now restart.");
                            System.Diagnostics.Process.Start(Application.ExecutablePath); // to start new instance of application
                            Environment.Exit(0);//to turn off current app
                           // Application.Restart();
                        }
                        catch (DeploymentDownloadException dde)
                        {
                            MessageBox.Show("Cannot install the latest version of the application. \n\nPlease check your network connection, or try again later. Error: " + dde);
                            return;
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("You are using the newest version of this software.","Info", MessageBoxButtons.OK, MessageBoxIcon.Information);            
            }
        }


        Thread ThreadStartFrm;
        string Navabarlayout = Application.StartupPath + "\\Layout\\";
        string setingpath = Application.StartupPath + "\\Setting.cf";
        string colortag = "/setting/Color";
        string SkinName = "";
        public static IList<CoreData.Permission> UserModuleRole;
        public static IList<CoreData.Permission> GroupModuleRole;
        //public static long UserID { get; set; }
        //public static long StaffID { get; set; }
        //public static string UserNname { get; set; }
        //public static string StaffName { get; set; }
        
        private void SetupSecurity()
        {
            frmLoading floading = new frmLoading();
            if (floading.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                frmLogin login = new frmLogin();
                if (login.ShowDialog() != System.Windows.Forms.DialogResult.Yes)
                {
                    navBarControl1.Visible = false;
                    barButtonItem_login.Enabled = true;
                    barButtonItem_logout.Enabled = false;
                    barSubItem_View.Enabled = false;

                }
                else
                {
                    {
                       
                        ValueDefine.UserNname = login.Username;
                        ValueDefine.UserID = login.UserID;
                        ValueDefine.StaffID = login.StaffID;
                        ValueDefine.StaffName = login.StaffName;

                        // barButtonItem_logout.Enabled = true;
                        navBarControl1.Visible = true;
                        barButtonItem_login.Enabled = false;
                        barButtonItem_logout.Enabled = true;
                        barSubItem_View.Enabled = true;
                        lbluser.Caption = login.Username;
                        lbldate.Caption = DateTime.Now.ToShortDateString();
                        //---get version ---            
                        if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
                        {
                            Version vs = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion;
                            lblversion.Caption = String.Format("{0}.{1}.{2}.{3}"
                               , vs.Major
                               , vs.Minor
                               , vs.Build
                               , vs.Revision);
                        }
                        else
                            lblversion.Caption = Application.ProductVersion;
                        //---end get version---


                        for (int i = 0; i < navBarControl1.Items.Count; i++)
                        {
                            if (navBarControl1.Items[i].Tag != null)
                            {
                                string tag = navBarControl1.Items[i].Tag.ToString();
                                var userrolecheck = UserModuleRole.FirstOrDefault(c => c.Module.Equals(tag));
                                var grouprolecheck = GroupModuleRole.FirstOrDefault(c => c.Module.Equals(tag));

                                if ((userrolecheck != null && userrolecheck.Role.Contains("R")) || (grouprolecheck != null && grouprolecheck.Role.Contains("R")))
                                {
                                    navBarControl1.Items[i].Visible = true;
                                }
                                else
                                {
                                    navBarControl1.Items[i].Visible = false;

                                }

                                //navBarControl1.Items[i].Visible = (permission != null && permission.Permission.Contains("R")) || (barStaticItem_UserName.Caption.ToLower() == "administrator");
                                navBarControl1.Items[i].Enabled = navBarControl1.Items[i].Visible;
                            }

                        }
                        //phan nay la de demo cho khách nên khóa lại

                        // navBarGroup5.Visible = false; //hoc vien
                        //     navBarGroup1.Visible = false; // danh muc
                        //  navBarGroup6.Visible = false; //kho
                        //navBarGroup7.Visible = false; thu quy
                        // navBarGroup8.Visible = false;//ke toan

                        //  navBarGroup4.Visible = false; //quan tri
                    }
                }
            }
        }

       void SetUpLanguage()
        {
        
        }

       private void barButtonItem_CheckNewVersion_ItemClick(object sender, ItemClickEventArgs e)
       {
           InstallUpdateSyncWithInfo();
       }


       private void AddUC(BaseActionService model, DetailUC control)
        {
            this.BeginInvoke((Action)(() =>
            {
                this.Cursor = Cursors.WaitCursor;
                frmMaster frm = new frmMaster();
                frm._Model = model;
                frm.DetailUCControl = control;
                frm.MdiParent = this;
                frm.Show();
                this.Cursor = Cursors.Arrow;
                ThreadStartFrm.Abort();
            }));
              
        }
        private void AddUC(BaseActionService model, DetailUC control, SearchUC Search)
        {
            this.BeginInvoke((Action)(() =>
            {
             this.Cursor = Cursors.WaitCursor;
            frmMaster frm = new frmMaster();
            frm._Model = model;
            frm.SearchUC = Search;
            frm.DetailUCControl = control;
            frm.MdiParent = this;
            frm.Show();
            this.Cursor = Cursors.Arrow;
            ThreadStartFrm.Abort();
            }));
           
        }
        private void AddUC(BaseActionService model, DetailUC control, SearchUC Search, string Searchstring)
        {

           
            this.BeginInvoke((Action)(() =>
            {
                this.Cursor = Cursors.WaitCursor;
                frmMaster frm = new frmMaster();
                frm._Model = model;
                frm.SearchUC = Search;
                frm.DetailUCControl = control;
                frm.MdiParent = this;
                frm.SearchString = Searchstring;
                frm.Show();
                this.Cursor = Cursors.Arrow;
                ThreadStartFrm.Abort();
            }));
           
         
        }
        /// <summary>
        /// Add user control
        /// </summary>
        /// <param name="model"></param>
        /// <param name="control"></param>
        /// <param name="search"></param>
        private void AddUC(BaseActionService model, DetailUC control, string search)
        {
            this.BeginInvoke((Action)(() =>
            {
             this.Cursor = Cursors.WaitCursor;
            frmMaster frm = new frmMaster();
            frm.SearchString = search;
            frm._Model = model;
            //control.Name = "";
            frm.DetailUCControl = control;
            frm.MdiParent = this;
            frm.Show();
            this.Cursor = Cursors.Arrow;
            ThreadStartFrm.Abort();
            }));
          
        }

        /// <summary>
        /// Add form
        /// </summary>
        /// <param name="form"></param>
        private void AddForm( DevExpress.XtraEditors.XtraForm form)
        {

            form.MdiParent = this;
            form.Show();
        }

        /// <summary>
        /// Kiểm tra form đã được khởi động hay chơi
        /// </summary>
        /// <param name="control"></param>
        /// <returns></returns>
        public bool ExistUC(DetailUC control)
        {

            for (int i = 0; i < this.MdiChildren.Length; i++)
            {
                try
                {
                    if (((frmMaster)MdiChildren[i]).DetailUCControl.Code == ((DetailUC)control).Code && ((frmMaster)MdiChildren[i]).DetailUCControl.GetType() == control.GetType())
                    {
                       
                        MdiChildren[i].Activate();
                        return true;
                    }
                }
                catch (Exception)
                {

                }
               
            }
            return false;
        }


      
        void RestoreLayout()
        {
            if (File.Exists(Navabarlayout + navBarControl1.Name + ".nava"))
            {
               // this.navBarControl1.RestoreFromXml(Navabarlayout + navBarControl1.Name + ".nava");
            }
            SkinName = FileHelper.GetValueFromXml(setingpath, colortag);
            if (!String.IsNullOrEmpty(SkinName))
            {
               
                defaultLookAndFeel1.LookAndFeel.SkinName = SkinName;
            }
           
        }
        void SaveLayout()
        {
            
            this.navBarControl1.SaveToXml(Navabarlayout + navBarControl1.Name + ".nava");
            FileHelper.SetValueForXml(setingpath, colortag, defaultLookAndFeel1.LookAndFeel.SkinName);
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            SaveLayout();
            Environment.Exit(0);
        }


        private void barButtonItem_config_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmConfig cf = new frmConfig();
            cf.ShowDialog();
        }
        

        private void barButtonItem_Restart_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            System.Diagnostics.Process.Start(Application.ExecutablePath); // to start new instance of application
            Environment.Exit(0);//to turn off current app
        }

        private void barButtonItem_login_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SetupSecurity();
        }

        private void navBarItem_Module_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            DetailUC control = new ModuleUC();
            if (!ExistUC(control))
            {
                
               ThreadStartFrm =  new Thread(new ThreadStart(()=>   AddUC(RegisterService.RegisterServices.ModuleService, control)));
               ThreadStartFrm.Start();
            }
        }

       

        private void barButtonItem_showdetail_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                frmMaster frm = (frmMaster)this.ActiveMdiChild;
                frm.ShowDetail();
            }
            catch (Exception)
            {

                //throw;
            }
        }

      

        private void barButtonItem_logout_ItemClick(object sender, ItemClickEventArgs e)
        {
         
            foreach (Form frm in this.MdiChildren)
            {
                frm.Close();
            }
          
            navBarControl1.Visible = false;
            barButtonItem_login.Enabled = true;
            barButtonItem_logout.Enabled = false;
            barButtonItem_changepass.Enabled = false;
            barSubItem_View.Enabled = false;
            lbluser.Caption = "";
            

        }

        private void barButtonItem_function_ItemClick(object sender, ItemClickEventArgs e)
        {
            dockPanel_function.Show();
        }

      

        private void navBarItem_group_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            DetailUC control = new GroupUC();
            if (!ExistUC(control))
            {
               ThreadStartFrm =  new Thread(new ThreadStart(()=> AddUC(RegisterService.RegisterServices.GroupService, control)));
               ThreadStartFrm.Start();
            }
        }

       
        private void navBarItem_user_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            DetailUC control = new UserUC();
            if (!ExistUC(control))
            {
               ThreadStartFrm =  new Thread(new ThreadStart(()=> AddUC(RegisterService.RegisterServices.UserService, control)));
               ThreadStartFrm.Start();
            }
          
        }

        private void navBarItem_nganh_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            BranchUC control = new BranchUC();
            if (!ExistUC(control))
            {
               ThreadStartFrm =  new Thread(new ThreadStart(()=> AddUC(RegisterService.RegisterServices.BranchService, control)));
               ThreadStartFrm.Start();
            }
        }

        private void navBarItem_Khoa_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            DetailUC control = new CourseUC();
            if (!ExistUC(control))
            {
              ThreadStartFrm =  new Thread(new ThreadStart(()=>  AddUC(RegisterService.RegisterServices.CourseService, control)));
              ThreadStartFrm.Start();
            }
        }

        private void navBarItem_Giaovien_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            TeacherUC control = new TeacherUC();
            if (!ExistUC(control))
            {
               ThreadStartFrm =  new Thread(new ThreadStart(()=> AddUC(RegisterService.RegisterServices.TeacherService, control)));
               ThreadStartFrm.Start();
            }
        }

        private void navBarItem_lophoc_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            ClassUC control = new ClassUC();
            if (!ExistUC(control))
            {
              ThreadStartFrm =  new Thread(new ThreadStart(()=>  AddUC(RegisterService.RegisterServices.ClassService, control)));
              ThreadStartFrm.Start();
            }
        }

        private void navBarItem_hocvientiennang_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            TempStudentUC control = new TempStudentUC();
            var searchUC = new TempStudentSearchUC();
            string SearchString = "Convert(varchar(11), TempStudent.ContactDate, 120) BETWEEN '" + new DateTime(DateTime.Now.Year-1,1,1).ToString("yyyy-MM-dd") + "' and '" + DateTime.Now.ToString("yyyy-MM-dd") + "'";
            if (!ExistUC(control))
            {
                ThreadStartFrm =  new Thread(new ThreadStart(()=>AddUC(RegisterService.RegisterServices.TempStudentService, control, searchUC, SearchString)));
                ThreadStartFrm.Start();
            }
        }

        private void navBarItem_hocvien_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            StudentUC control = new StudentUC();
            var searchUC = new StudentSearchUC();
            string SearchString = " Convert(varchar(11), st.DateOfAdmission, 120) BETWEEN '" + DateTime.Now.AddMonths(-18).ToString("yyyy-MM-dd") + "' and '" + DateTime.Now.AddMonths(3).ToString("yyyy-MM-dd") + "'";
            if (!ExistUC(control))
            {
              
              ThreadStartFrm =  new Thread(new ThreadStart(()=>AddUC(RegisterService.RegisterServices.StudentService, control, searchUC, SearchString )));
              ThreadStartFrm.Start();
            }
        }

      

        private void navBarItem_theodoihocphi_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            Tracking_Student_FeeUC control = new Tracking_Student_FeeUC();
            var searchUC = new Student_accounting_SearchUC();
            string SearchString = " Convert(varchar(11), Student.DateOfAdmission, 120) BETWEEN '" + DateTime.Now.AddMonths(-6).ToString("yyyy-MM-dd") + "' and '" + DateTime.Now.ToString("yyyy-MM-dd") + "'";
            if (!ExistUC(control))
            {
              ThreadStartFrm =  new Thread(new ThreadStart(()=>  AddUC(RegisterService.RegisterServices.Student_FeeService, control, searchUC,SearchString)));
              ThreadStartFrm.Start();
            }
        }

        private void navBarItem_monhoc_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            SubjectUC control = new SubjectUC();
            if (!ExistUC(control))
            {
              ThreadStartFrm =  new Thread(new ThreadStart(()=>  AddUC(RegisterService.RegisterServices.SubjectService, control)));
              ThreadStartFrm.Start();
            }
        }

        private void navBarItem_cotdiem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            PointColumnUC control = new PointColumnUC();
            if (!ExistUC(control))
            {
               ThreadStartFrm =  new Thread(new ThreadStart(()=> AddUC(RegisterService.RegisterServices.PointColumnService, control)));
               ThreadStartFrm.Start();
            }
        }

        private void navBarItem_hocvien_diem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            Student_ResultUC control = new Student_ResultUC();
            var searchUC = new Student_ResultSearchUC();
            string SearchString ="no";
            if (!ExistUC(control)) 
            {
                //đang lẽ là điểm thì chỉ load lớp chuyên nghiệp thôi tức là c.type==1 nhưng do để tăng tốc độ ta buộc phải cho giáo viên chọn lớp trước
                //nên khi khởi động ta để searchtring =no đồng nghĩa với ko cho nạp lúc khởi đông
                ThreadStartFrm = new Thread(new ThreadStart(() => AddUC(RegisterService.RegisterServices.StudentService, control, searchUC, SearchString)));
               ThreadStartFrm.Start();
            }
        }

      
       

        //thoat ra save lai layout hien hanh cua phan mem
        private void barButtonItem3_ItemClick(object sender, ItemClickEventArgs e)
        {
            SaveLayout();
            Environment.Exit(0);
        }

        private void navBarItem_sanpham_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            DetailUC control = new ProductUC();
            var searchUC = new ProductSearchUC();
            string SearchString = "1=1";
            if (!ExistUC(control))
            {
                ThreadStartFrm = new Thread(new ThreadStart(() => AddUC(RegisterService.RegisterServices.ProductService, control, searchUC, SearchString)));
              ThreadStartFrm.Start();
            }
        }

        private void navBarItem_xuatkho_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            Stock_VoucherOut_List frm = new Stock_VoucherOut_List();
            AddForm(frm);
        }

        private void navBarItem_nhapkho_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            Stock_VoucherIn_List frm = new Stock_VoucherIn_List();
            AddForm(frm);
        }

        private void navBarItem_tonkho_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            //Inventory frm = new Inventory();
            //AddForm(frm);
        }

        private void navBarItem_nhomsanpham_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            DetailUC control = new Product_GroupUC();
            if (!ExistUC(control))
            {
                ThreadStartFrm =  new Thread(new ThreadStart(()=>AddUC(RegisterService.RegisterServices.ProductGroupService, control)));
                ThreadStartFrm.Start();
            }
        }

        private void navBarItem_thekho_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            Stock_ReportForm frm = new Stock_ReportForm();
            AddForm(frm);
        }

        private void navBarItem_chuyenkho_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            //Stock_TranferList frm = new Stock_TranferList();
            //AddForm(frm);
        }

        private void navBarItem_thu_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
           
          
        }

        private void navBarItem_chi_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            //DetailUC control = new PaymentUC();
            //if (!ExistUC(control))
            //{
            //  //  AddUC(RegisterService.RegisterServices.PaymentVoucherService, control);
            //}
        }

        private void navBarItem_donvi_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            DetailUC control = new Product_UnitUC();
            if (!ExistUC(control))
            {
               ThreadStartFrm =  new Thread(new ThreadStart(()=> AddUC(RegisterService.RegisterServices.ProductUnitService, control)));
               ThreadStartFrm.Start();
            }
        }

        private void navBarItem_nhacungcap_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            DetailUC control = new Product_ProviderUC();
            if (!ExistUC(control))
            {
               ThreadStartFrm =  new Thread(new ThreadStart(()=> AddUC(RegisterService.RegisterServices.ProductProviderService, control)));
               ThreadStartFrm.Start();
            }
        }

        private void navBarItem_nhanvien_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            DetailUC control = new StaffUC();
            if (!ExistUC(control))
            {
               ThreadStartFrm =  new Thread(new ThreadStart(()=> AddUC(RegisterService.RegisterServices.StaffService, control)));
               ThreadStartFrm.Start();
            }
        }

        private void navBarItem_lydothu_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            DetailUC control = new Receipt_Payment_ReasonUC(1);
            if (!ExistUC(control))
            {
              ThreadStartFrm =  new Thread(new ThreadStart(()=>  AddUC(RegisterService.RegisterServices.PaymentReasonService, control," Type=1")));
              ThreadStartFrm.Start();
            }
        }

        private void navBarItem_lydochi_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            DetailUC control = new Receipt_Payment_ReasonUC(2);
            if (!ExistUC(control))
            {
              ThreadStartFrm =  new Thread(new ThreadStart(()=>  AddUC(RegisterService.RegisterServices.PaymentReasonService, control, " Type=2")));
              ThreadStartFrm.Start();
            }
        }

        private void navBarItem_phuongthucthanhtoan_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            DetailUC control = new Payment_MethodUC();
            if (!ExistUC(control))
            {
               ThreadStartFrm =  new Thread(new ThreadStart(()=> AddUC(RegisterService.RegisterServices.PaymentMethodService, control)));
               ThreadStartFrm.Start();
            }
        }

    

        private void navBarItem_thudungthu_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            DetailUC control = new Receipt_VoucherSaleUC();
            if (!ExistUC(control))
            {
                ThreadStartFrm = new Thread(new ThreadStart(() => AddUC(RegisterService.RegisterServices.ReceiptVoucherSaleService, control)));
                ThreadStartFrm.Start();
            }
        }

        private void navBarItem_Phuongthucnhap_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            DetailUC control = new StockMethodUC();
            if (!ExistUC(control))
            {
                ThreadStartFrm = new Thread(new ThreadStart(() => AddUC(RegisterService.RegisterServices.StockMethodService, control)));
                ThreadStartFrm.Start();
            }
        }

        private void navBarItem_phuongthucxuat_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            //DetailUC control = new StockMethodUC();
            //if (!ExistUC(control))
            //{
            //    ThreadStartFrm = new Thread(new ThreadStart(() => AddUC(RegisterService.RegisterServices.StockMethodService, control)));
            //    ThreadStartFrm.Start();
            //}
        }

        private void navBarItem_kho_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            DetailUC control = new StockUC();
            if (!ExistUC(control))
            {
                ThreadStartFrm = new Thread(new ThreadStart(() => AddUC(RegisterService.RegisterServices.ProductWareHouseService, control)));
                ThreadStartFrm.Start();
            }
        }

        private void navBarItem_studentReport_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            frmReportStudent st = new frmReportStudent();
            AddForm(st);
        }

        private void navBarItem_Thuhocphihocvien_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            DetailUC control = new Receipt_VoucherFeeUC();
            if (!ExistUC(control))
            {
                string SearchString = " Voucher.ReceiptReason =1 or Voucher.ReceiptReason =8 and Voucher.Type=0 ";
               ThreadStartFrm =  new Thread(new ThreadStart(()=> AddUC(RegisterService.RegisterServices.VoucherService, control, SearchString)));
               ThreadStartFrm.Start();
            }
        }

        private void navBarItem_baocaoketoan_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            frmReportAccounting frm = new frmReportAccounting();
            AddForm(frm);
        }

        private void navBarItem_thucanho_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            DetailUC control = new Receipt_VoucherApartmentUC();
            if (!ExistUC(control))
            {
              ThreadStartFrm =  new Thread(new ThreadStart(()=>  AddUC(RegisterService.RegisterServices.ReceiptVoucherApartmentService, control)));
              ThreadStartFrm.Start();
            }
        }

        private void navBarItem_thunoibo_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            DetailUC control = new Receipt_VoucherLocalUC();
            if (!ExistUC(control))
            {
               ThreadStartFrm =  new Thread(new ThreadStart(()=> AddUC(RegisterService.RegisterServices.ReceiptVoucherLocalService, control)));
               ThreadStartFrm.Start();
            }
        }

        private void navBarItem_thusalon_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            DetailUC control = new Receipt_VoucherSalonUC();
            if (!ExistUC(control))
            {
               ThreadStartFrm =  new Thread(new ThreadStart(()=> AddUC(RegisterService.RegisterServices.ReceiptVoucherSalonService, control)));
               ThreadStartFrm.Start();
            }
        }

        private void navBarItem_chicanho_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            DetailUC control = new Payment_VoucherApartmentUC();
            if (!ExistUC(control))
            {
              ThreadStartFrm =  new Thread(new ThreadStart(()=>  AddUC(RegisterService.RegisterServices.PaymentVoucherApartmentService, control)));
              ThreadStartFrm.Start();
            }
        }

        private void navBarItem_chitruong_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            DetailUC control = new Payment_VoucherSchoolUC();
            if (!ExistUC(control))
            {
              ThreadStartFrm =  new Thread(new ThreadStart(()=>  AddUC(RegisterService.RegisterServices.PaymentVoucherSchoolService, control)));
              ThreadStartFrm.Start();
            }
        }

        private void barButtonItem_changepass_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmChangePass f = new frmChangePass();
            f.ShowDialog();
        }

        private void navBarItem_thongkethu_chi_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            AddForm(new frmCashBook());
        }

        private void navBarItem_kenhthongtin_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            ChannelInfoUC control = new ChannelInfoUC();
            if (!ExistUC(control))
            {
               ThreadStartFrm =  new Thread(new ThreadStart(()=> AddUC(RegisterService.RegisterServices.ChannelInfoService, control)));
               ThreadStartFrm.Start();
            }
        }

        private void navBarItem_trangthaihocvien_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            DetailUC control = new Student_Status_UC(1);

            if (!ExistUC(control))
            {
              ThreadStartFrm =  new Thread(new ThreadStart(()=>  AddUC(RegisterService.RegisterServices.StudentStatusService, control, "Type=1")));
              ThreadStartFrm.Start();
            }
        }

        private void navBarItem_trangthaihocvientiemnang_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            DetailUC control = new Student_Status_UC(2);

            if (!ExistUC(control))
            {
               ThreadStartFrm =  new Thread(new ThreadStart(()=> AddUC(RegisterService.RegisterServices.StudentStatusService, control, "Type=2")));
               ThreadStartFrm.Start();
            }
        }

        private void barButtonItem_report_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmReportError r = new frmReportError();
            r.Show();
        }

        private void barButtonItem_EmailForm_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmEmailConfig email = new frmEmailConfig();
            email.ShowDialog();
        }

        private void navBarItem_KK_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            frmStockBegin frm = new frmStockBegin();
            AddForm(frm);
        }

        private void navBarItem_KiemKePhongHoc_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            frmInventory frm = new frmInventory();
            AddForm(frm);
        }


        private void navBarItem_baocaohvtiemnang_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            frmReportTempStudent frm = new frmReportTempStudent();
            AddForm(frm);
        }

        private void navBarItem_dacoc_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            DetailUC control = new TempStudentDepositUC();
            if (!ExistUC(control))
            {
                ThreadStartFrm = new Thread(new ThreadStart(() => AddUC(RegisterService.RegisterServices.TempStudentDepositInfoService, control)));
                ThreadStartFrm.Start();
            }
        }

          

      

       
    }
}
